/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package View;

import ControllerInterface.IControllerCrawler;
import ViewInterface.IViewCrawler;
import javax.swing.JOptionPane;

/**
 *
 * @author Fabian
 */
public class ViewCrawler extends javax.swing.JFrame implements IViewCrawler {
    
    IControllerCrawler myController;
    int seg;
    int min;
    int hora;
    int go;
    Thread hilo;
    
    /**
     * 
     */
    public ViewCrawler() {
        initComponents();
        jPanelCrawling.setVisible(false);
        
    }
    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jcbxCrawler = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jbtnExtract = new javax.swing.JButton();
        jbtnInsert = new javax.swing.JButton();
        jcbxXMLfiles = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        jPanelCrawling = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        jbtnStopCrawler = new javax.swing.JButton();
        jLabelTime = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Extraccion de datos");

        jLabel1.setText("Extracción de datos");

        jLabel2.setText("Seleccione el extractor de datos que utilizará:");

        jbtnExtract.setText("Iniciar extracción");
        jbtnExtract.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnExtractActionPerformed(evt);
            }
        });

        jbtnInsert.setText("Mapeo de datos");
        jbtnInsert.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnInsertActionPerformed(evt);
            }
        });

        jLabel3.setText("Seleccione los datos a mapear:");

        jLabel4.setText("Extracción en curso");

        jbtnStopCrawler.setText("Detener extracción");
        jbtnStopCrawler.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnStopCrawlerActionPerformed(evt);
            }
        });

        jLabelTime.setText("jLabel5");

        javax.swing.GroupLayout jPanelCrawlingLayout = new javax.swing.GroupLayout(jPanelCrawling);
        jPanelCrawling.setLayout(jPanelCrawlingLayout);
        jPanelCrawlingLayout.setHorizontalGroup(
            jPanelCrawlingLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelCrawlingLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelCrawlingLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanelCrawlingLayout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jbtnStopCrawler, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanelCrawlingLayout.createSequentialGroup()
                        .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 180, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabelTime)
                        .addGap(45, 45, 45))))
        );
        jPanelCrawlingLayout.setVerticalGroup(
            jPanelCrawlingLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelCrawlingLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelCrawlingLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jLabelTime))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jbtnStopCrawler)
                .addContainerGap(13, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jcbxCrawler, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addContainerGap())))
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jcbxXMLfiles, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addGap(0, 0, Short.MAX_VALUE)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jbtnExtract, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 159, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jbtnInsert, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 158, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 250, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(0, 48, Short.MAX_VALUE))
                            .addComponent(jPanelCrawling, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(111, 111, 111)
                        .addComponent(jLabel1)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(32, 32, 32)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jcbxCrawler, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jbtnExtract)
                .addGap(18, 18, 18)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jcbxXMLfiles, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(7, 7, 7)
                .addComponent(jbtnInsert, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanelCrawling, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    //Boton que inicia el proceso de mapeo de datos desde XML
    private void jbtnInsertActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnInsertActionPerformed
        String xmlFile = null;
        try {
            xmlFile = this.jcbxXMLfiles.getItemAt(this.jcbxXMLfiles.getSelectedIndex()).toString();
        } catch (Exception e) {
            
        }
        
        this.myController.initMapCrawler(xmlFile);
    }//GEN-LAST:event_jbtnInsertActionPerformed
    
    private void jbtnExtractActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnExtractActionPerformed
        this.jLabel4.setText("Extracción en curso");
        hora = 0;
        min = 0;
        seg = 0;
        go = JOptionPane.showConfirmDialog(this,
                "Comenzará el proceso de extracción de datos ¿Desea continuar?",
                "Confirmar extracción de datos",
                JOptionPane.YES_NO_OPTION);
        jPanelCrawling.setVisible(true);
        
        timeCount();
        hilo.start();
        
        new Thread() {
            public void run() {
                if (go == 0) {
                    jPanelCrawling.setVisible(true);
                    boolean ok = myController.initExtraction(jcbxCrawler.getSelectedIndex());
                    if (ok) {
                        hilo.stop();
                        JOptionPane.showMessageDialog(null, "Extracción finalizada",
                                "Extracción realizada con éxito",
                                JOptionPane.INFORMATION_MESSAGE);
                    } else {
                        hilo.stop();
                        JOptionPane.showMessageDialog(null, "Error",
                                "Error en la extracción",
                                JOptionPane.INFORMATION_MESSAGE);
                    }
                }
                jPanelCrawling.setVisible(false);
            }
        ;
    }.start();
        
        
    }//GEN-LAST:event_jbtnExtractActionPerformed

private void jbtnStopCrawlerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnStopCrawlerActionPerformed
    this.jLabel4.setText("Finalizando extracción");
    this.myController.stopExtraction();
    }//GEN-LAST:event_jbtnStopCrawlerActionPerformed
    
    private void timeCount() {
        hilo = new Thread() {

            public void run() {
                try {
                    while (true) {
                        if (seg == 59) {
                            seg = 0;
                            min++;
                        }
                        if (min == 59) {
                            min = 0;
                            hora++;
                        }
                        seg++;
                        jLabelTime.setText(hora + ":" + min + ":" + seg);
                        sleep(1000);
                    }
                } catch (java.lang.InterruptedException ie) {
                    System.out.println(ie.getMessage());
                }
            }
        };
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ViewCrawler.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ViewCrawler.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ViewCrawler.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ViewCrawler.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {
            
            public void run() {
                new ViewCrawler().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabelTime;
    private javax.swing.JPanel jPanelCrawling;
    private javax.swing.JButton jbtnExtract;
    private javax.swing.JButton jbtnInsert;
    private javax.swing.JButton jbtnStopCrawler;
    private javax.swing.JComboBox jcbxCrawler;
    private javax.swing.JComboBox jcbxXMLfiles;
    // End of variables declaration//GEN-END:variables

    /**
     * 
     * @param controllerCrawler
     */
    @Override
    public void setController(IControllerCrawler controllerCrawler) {
        myController = controllerCrawler;
        this.setVisible(true);
    }
    
    /**
     * 
     * @param crawlerName
     */
    @Override
    public void setCrawlerName(String crawlerName) {
        this.jcbxCrawler.addItem(crawlerName);
        this.jcbxCrawler.repaint();
    }
    
    /**
     * 
     * @param XMLName
     */
    @Override
    public void setXMLName(String XMLName) {
        this.jcbxXMLfiles.addItem(XMLName);
        this.jcbxXMLfiles.repaint();
    }
}
